package ran.quzitech.rnutils.manager;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableNativeMap;

import ran.quzitech.rnutils.MsgCode;
import ran.quzitech.rnutils.utils.SpUtils;


/**
 * Created by Administrator on 2016/12/2 0002.
 */
public class DBManager extends ReactContextBaseJavaModule {

    private Context context;
    private ReactContext reactContext;
    private final String userPrefix = "user_";
    private final String requestPrefix = "request_";
    private final String request_id = "request_id";

    @Override
    public String getName() {
        return "DBManager";
    }

    public DBManager(ReactApplicationContext reactContext) {
        super(reactContext);
        context = reactContext;
        this.reactContext = reactContext;


    }

    @ReactMethod
    public void saveLocally(String key, String value, Callback callback) {
        Log.v("DBManager", value);
        SpUtils.put(context, userPrefix + key, value);
        callback.invoke();
    }

    @ReactMethod
    public void saveDictLocally(String key, ReadableMap map, Callback callback) {
        Log.v("DBManager", map+"");
        SpUtils.put(context, userPrefix + key, JSON.toJSONString(((ReadableNativeMap) map).toHashMap()));
        callback.invoke();
    }

    @ReactMethod
    public void readLocally(String key, Callback callback) {
        callback.invoke(null, SpUtils.get(context, userPrefix + key, MsgCode.getErrorMap(MsgCode.Fail_to_read).toString()).toString());

    }

}
